package com.bbx.taxi.client.widget.Dailog;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.bbx.androidapi.util.DensityUtil;
import com.bbx.api.util.LogUtils;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Util.IsActivityFinishUtils;
import com.bbx.taxi.client.widget.MaxByteLengthEditText;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickLeftListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @Path com.bbx.taxi.client.widget.Dailog.MySaveMoneyDialog.java
 * @Author 严旭文
 * @Date 2016-6-7 下午7:05:19
 * @Company 蓝海（福建）信息技术有限公司
 * @Description 省钱
 */
public class MySaveMoneyDialog extends Dialog   {
	public LinearLayout layout_dailog;
	private LinearLayout layout_double;
	private TextView tv_title;
	private TextView tv_content;
	private ListView listview;
	private MyAdapter mMyAdapter;
	private boolean cancelable = true;
	private Context context;
	private boolean isFinish = false;
	private List<String> list;
	private List<Integer> type;

	public MySaveMoneyDialog(Context context) {
		super(
				context,
				(MessageDialog.map_dialog.get(context.toString()) != null ? MessageDialog.map_dialog
						.get(context.toString()) : 0) == 0 ? R.style.Dialog_bocop
						: R.style.Dialog_bocop2);
		this.context = context;
		list = new ArrayList<String>();
		type = new ArrayList<Integer>();
		init();
	}

	private void init() {
		View contentView = View.inflate(context, R.layout.dailog_savemoney,
				null);
		setContentView(contentView);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_content = (TextView) findViewById(R.id.tv_content);
		listview = (ListView) findViewById(R.id.listview);
		mMyAdapter = new MyAdapter(context, list, type);
		listview.setAdapter(mMyAdapter);
		layout_dailog = (LinearLayout) findViewById(R.id.layout_dailog);
		layout_dailog.setVisibility(View.GONE);
		this.setCancelable(false);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				dismiss();
				if (mMyOnItemClickListener != null)
					mMyOnItemClickListener.onItemClick(parent, view, position, id);
			}
		});
		contentView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelable) {
					dismiss();
				}
			}
		});
	}

	@Override
	public void show() {
		if (!IsActivityFinishUtils.isActivityFinish(context)) {
			if (MessageDialog.map_dialog != null) {
				int size = MessageDialog.map_dialog.get(context.toString()) != null ? MessageDialog.map_dialog
						.get(context.toString()) : 0;
				MessageDialog.map_dialog.put(context.toString(), size + 1);
				if (MessageDialog.map_dialog.get(context.toString()) != null
						&& MessageDialog.map_dialog.get(context.toString()) <= MessageDialog.max_dialog) {
					super.show();
					Animation a = AnimationUtils.loadAnimation(context,
							R.anim.dialog_in);
					layout_dailog.startAnimation(a);
					layout_dailog.setVisibility(View.VISIBLE);
					isFinish = false;
				} else {
					MessageDialog.map_dialog
							.put(context.toString(), MessageDialog.map_dialog
									.get(context.toString()) - 1);
				}
			}
		}
	}

	@SuppressLint("NewApi")
	@Override
	public void dismiss() {
		if (!IsActivityFinishUtils.isActivityFinish(context)) {
			if (isFinish)
				super.dismiss();
			int size = MessageDialog.map_dialog.get(context.toString()) != null ? MessageDialog.map_dialog
					.get(context.toString()) : 0;
			if (size > 0) {
				MessageDialog.map_dialog.put(context.toString(), size - 1);
			}

			Animation a = AnimationUtils.loadAnimation(context,
					R.anim.dialog_out);
			layout_dailog.startAnimation(a);
			a.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation anim) {
				}

				@Override
				public void onAnimationRepeat(Animation anim) {
				}

				@Override
				public void onAnimationEnd(Animation anim) {
					isFinish = true;
					dismiss();
				}
			});
		}

	}

	@Override
	public void setCancelable(boolean flag) {
		cancelable = flag;
		super.setCancelable(flag);
	}

	public void setTitle(String text) {
		if (text == null || text.equals("")) {
			tv_title.setVisibility(View.GONE);
		} else {
			tv_title.setVisibility(View.VISIBLE);
			tv_title.setText(text);
		}

	}

	public void setContent(String text) {
		if (text == null || text.equals("")) {
			tv_content.setVisibility(View.GONE);
		} else {
			tv_content.setVisibility(View.VISIBLE);
			tv_content.setText(text);
		}

	}

	public void setList(List<String> list, List<Integer> type) {
		this.list.clear();
		this.type.clear();
		this.list.addAll(list);
		this.type.addAll(type);
		setHeight(this.list.size());
		mMyAdapter.notifyDataSetChanged();

	}

	public class MyAdapter extends BaseAdapter {
		private List<String> list;
		private List<Integer> type;
		private Context context;

		public MyAdapter(Context context, List<String> list, List<Integer> type) {
			this.list = list;
			this.type = type;
			this.context = context;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewholder;
			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(context);
				convertView = (View) inflater.inflate(R.layout.button, null);
				viewholder = new ViewHolder();
				viewholder.btn = (TextView) convertView.findViewById(R.id.btn);

				convertView.setTag(viewholder);

			} else
				viewholder = (ViewHolder) convertView.getTag();
			viewholder.btn.setText(list.get(position));
			return convertView;

		}

		public class ViewHolder {
			TextView btn;
		}
	}

	public interface MyOnItemClickListener {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id);
	}

	public MyOnItemClickListener mMyOnItemClickListener;

	public void setOnItemClickListener(MyOnItemClickListener l) {
		mMyOnItemClickListener = l;
	}
	public void setHeight(int size){
		listview.setVisibility(View.GONE);
		//获取列表项的大小
				LayoutInflater inflater = LayoutInflater.from(context);
				View item_view = (View) inflater.inflate(R.layout.button, null);
				//外层布局要LinearLayout
				item_view.measure(0,
						MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			   int h = (item_view.getMeasuredHeight()*size)+DensityUtil.dip2px(context, size*10+10);
				//设置ListView的大小
				RelativeLayout.LayoutParams linearParams =(RelativeLayout.LayoutParams) listview.getLayoutParams();
				linearParams.height=h;
				listview.setLayoutParams(linearParams);
				listview.setVisibility(View.VISIBLE);
	}
}